<template>
  <div class="product-page-container" style="margin-top: 20px">
    <div class="product-detail-container">
      <!-- 产品图片部分 -->
      <div class="product-image-section" style="margin-top: 80px">
        <el-image :src="productDetail.productImg" fit="cover" class="product-image"
                  style="max-width: 500px;max-height: 500px;"/>
      </div>
      <!-- 产品信息部分 -->
      <div class="product-info-section" style="margin-top: 130px">
            <!-- 产品图片与信息 -->
        <h1 class="product-title">{{ productDetail.productName }}</h1>
        <p class="product-price">￥{{ (productDetail.sellingPrice / 100).toFixed(2) }}</p>
        <ul class="product-features">
          <li>型号: {{ productDetail.modelNumber }}</li>
          <li>库存: {{ productDetail.stock }} {{ productDetail.unit }}</li>
          <li>描述: {{ productDetail.productDescription }}</li>
        </ul>
        <div>
          <el-button @click="router.push('/')">
          <el-icon><ArrowLeft /></el-icon>
        </el-button>
            <el-button
          type="primary"
          size="large"
          @click="addOrderProductDetail"
        >
          立即下单
        </el-button>
        </div>
      </div>
       <el-dialog v-model="orderVisible" width="200" draggable overflow top="50vh">
      <template #header>
        <div class="dialog-header">
          <div class="dialog-title">用户下单</div>
        </div>
      </template>
      <img :src="productDetail.productImg" alt="preview Img"
           style="width: 100px;height: 100px;margin-left: 42px"/>
      <div>
        产品名称 : <span>{{ productDetail.productName }}</span>
      </div>
         <el-form-item label="购买数量">
          <el-input-number v-model="orderDTO.productNum" min="1" />
        </el-form-item>
      <div style="margin-top: 10px">
        邮箱编码 :
        <el-input
            v-model="orderDTO.postcode"
            style="width: 100px"
            size="small"
            placeholder="请输入邮箱编码"
        />
      </div>
      <el-button type="primary" size="small" style="margin-left: 95px;margin-top: 10px" @click="order">立即下单
      </el-button>
    </el-dialog>
    </div>

    <!-- 供应商信息与商品分类卡片 -->
    <div class="additional-info-container">
      <!-- 供应商信息部分 -->
      <div class="supplier-info-section">
        <h2 class="card-title">供应商信息</h2>
        <ul class="supplier-info-list">
          <li>供应商名称: {{ productDetail.supplier.boosName }}</li>
          <li>联系方式: {{  productDetail.supplier.phone}}</li>
          <li>供应商简介: {{  productDetail.supplier.introduction }}</li>
          <li>品牌名称: {{ productDetail.brand.brandName}}</li>
          <li>品牌图片:
            <div class="product-image-section">
            <el-image :src="productDetail.brand.brandImg"
                      fit="cover" class="product-image"
                      style="max-width: 100px; max-height: 100px;"/>
            </div>
          </li>
          <li>品牌官网:<a :href="productDetail.brand.brandNet">
            {{productDetail.brand.brandNet}}</a></li>
          <li>品牌名称: {{ productDetail.brand.brandName}}</li>
        </ul>
      </div>

      <!-- 商品分类与规格部分 -->
      <div class="product-category-spec-section">
        <h2 class="card-title">商品分类与规格</h2>
        <ul class="category-spec-list">
          <li>父级分类: {{ productDetail.categoryAndStandardsVO.categoryParentName }}</li>
          <li>子级分类: {{ productDetail.categoryAndStandardsVO.categoryChildName }}</li>
          <li> 分类样式图:
            <div class="product-image-section">
            <el-image :src="productDetail.categoryAndStandardsVO.categoryImg"
                      fit="cover" class="product-image"
                      style="max-width: 100px; max-height: 100px;"/>
          </div>
          </li>
          <li v-if="productDetail.categoryAndStandardsVO.standards.chemicalComposition">
            化学成分: {{ productDetail.categoryAndStandardsVO.standards.chemicalComposition }}</li>
          <li v-if="productDetail.categoryAndStandardsVO.standards.content">
            内容: {{ productDetail.categoryAndStandardsVO.standards.content}}</li>
          <li v-if="productDetail.categoryAndStandardsVO.standards.purity">
            纯度: {{ productDetail.categoryAndStandardsVO.standards.purity }}</li>
          <li v-if="productDetail.categoryAndStandardsVO.standards.performance">
            性能: {{ productDetail.categoryAndStandardsVO.standards.performance }}</li>
          <li v-if="productDetail.categoryAndStandardsVO.standards.capacity">
            容量(L): {{ productDetail.categoryAndStandardsVO.standards.capacity }}</li>
          <li v-if="productDetail.categoryAndStandardsVO.standards.length">
            长度(cm): {{ productDetail.categoryAndStandardsVO.standards.length }}</li>
          <li v-if="productDetail.categoryAndStandardsVO.standards.diameter">
            直径(cm): {{ productDetail.categoryAndStandardsVO.standards.diameter}}</li>
        </ul>
      </div>
    </div>
  </div>
</template>
<style scoped>
/* 整体页面布局 */
.product-page-container {
  width: 100vw;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  align-items: center;
  background-color: #f4f5f7;
  padding: 20px;
  box-sizing: border-box;
}

/* 产品详情部分 */
.product-detail-container {
  display: flex;
  flex-direction: row;
  justify-content: center;
  align-items: center;
  width: 90vw;
  max-width: 1200px;
  min-height: 300px; /* 设置最小高度，避免溢出 */
  background: #ffffff;
  border-radius: 16px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  margin-bottom: 20px;
}

/* 产品图片部分 */
.product-image-section {
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
  background: #f9f9f9;
  padding: 20px;
}

.product-image {
  width: 80%;
  height: auto;
  border-radius: 8px;
  object-fit: cover;
}

/* 产品信息部分 */
.product-info-section {
  flex: 1;
  padding: 40px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  text-align: left;
}

.product-title {
  font-size: 32px;
  font-weight: bold;
  margin-bottom: 20px;
}

.product-price {
  font-size: 28px;
  color: #e60012;
  font-weight: bold;
  margin-bottom: 20px;
}

.product-features {
  list-style-type: none;
  padding: 0;
  margin: 0 0 30px;
}

.product-features li {
  font-size: 16px;
  line-height: 1.8;
  color: #666;
}

/* 供应商信息与分类规格卡片 */
.additional-info-container {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: flex-start;
  width: 90vw;
  max-width: 1200px;
  background: #ffffff;
  border-radius: 16px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
  padding: 20px;
  box-sizing: border-box;
  gap: 20px; /* 卡片之间的间距 */
}

/* 供应商信息部分 */
.supplier-info-section,
.product-category-spec-section {
  flex: 1;
  padding: 20px;
  background-color: #f9f9f9; /* 卡片背景色略微不同 */
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
  overflow: auto; /* 在内容超出时显示滚动条 */
  max-height: 400px; /* 限制卡片高度 */
}

.card-title {
  font-size: 24px;
  font-weight: bold;
  margin-bottom: 20px;
  color: #333;
}

.supplier-info-list,
.category-spec-list {
  list-style-type: none;
  padding: 0;
  margin: 0;
}

.supplier-info-list li,
.category-spec-list li {
  font-size: 16px;
  line-height: 1.8;
  color: #666;
  margin-bottom: 8px;
}

.product-category-spec-section .product-image-section {
  display: flex;
  justify-content: flex-start;
  align-items: center;
  margin: 10px 0;
}

.product-category-spec-section .product-image {
  max-width: 100px;
  max-height: 100px;
  border-radius: 4px;
  margin-right: 10px;
}
.additional-info-container .product-image-section {
  display: flex;
  justify-content: flex-start;
  align-items: center;
  margin: 10px 0;
}

.additional-info-container .product-image {
  max-width: 100px;
  max-height: 100px;
  border-radius: 4px;
  margin-right: 10px;
}
</style>

<script setup>
import {onMounted, ref} from 'vue';
import {useRoute, useRouter} from 'vue-router';
import {getProductDetail} from "@/api/product.js";
import {addOrder} from '@/api/user.js'
import {useTokenStore} from "@/store/tokenStore.js";
import {ElButton, ElMessage} from "element-plus";
import {ArrowLeft} from "@element-plus/icons-vue";
const router = useRouter();
const route = useRoute();
const productId = ref(route.params.productId);
const orderVisible = ref(false);
const orderDTO = ref({
      productId: productId,
      productNum: 1,
      postcode: ""
    }
);
const productDetail = ref({
  brand: {},
  supplier: {},
  categoryAndStandardsVO: {
    standards: {}
  },
  productName: '',
  modelNumber: '',
  unit: '',
  sellingPrice: 0,
  stock: 0,
  productImg: '',
  productDescription: ''
});
const order = async () => {
  if(useTokenStore().token!==''){
    if(orderDTO.value.productNum<=productDetail.value.stock){
    await addOrder(orderDTO.value);
    orderVisible.value = false;
    router.push('/order')
    }else {
      ElMessage({
        showClose: true,
        message: '库存不足',
        type: 'error',
      })
    }
  }else {
    router.push('/')
    ElMessage({
      showClose: true,
      message: '请先登录',
      type: 'error',
    })
  }

}
const addOrderProductDetail = () => {
  orderVisible.value = true;
}
onMounted(async () => {
  // console.log(response)
  productDetail.value = await getProductDetail(productId.value);
  console.log(productDetail)
});
</script>